package com.edu.zju.scs.panacea.service.resource;

import com.edu.zju.scs.panacea.common.Global;
import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.query.LocQuery;
import com.edu.zju.scs.panacea.entity.query.PositionQuery;
import com.edu.zju.scs.panacea.entity.resource.Loc;
import com.edu.zju.scs.panacea.entity.vo.LocVO;
import com.edu.zju.scs.panacea.mapper.resource.LocMapper;
import com.edu.zju.scs.panacea.mapper.resource.PositionMapper;
import com.edu.zju.scs.panacea.mapper.resource.TemplateRelLocMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-20 14:37
 */
@Service
public class LocService {

    @Autowired
    private LocMapper locMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private TemplateRelLocMapper templateRelLocMapper;

    public BaseResult getLoc(Long id) {
        BaseResult baseResult = new BaseResult();
        Loc loc = locMapper.selectLoc(id);
        if (loc == null) {
            baseResult.error("无此id对应的资源位");
        } else {
            baseResult.success(loc);
        }
        return baseResult;
    }

    public BaseResult getAllLoc() {
        BaseResult baseResult = new BaseResult();
        baseResult.success(locMapper.selectAllLoc());
        return baseResult;
    }

    public BaseResult deleteLoc(Long id) {
        BaseResult baseResult = new BaseResult();
        long deleteNums = locMapper.deleteLoc(id);
        if (deleteNums == 0) {
            baseResult.error("删除失败！");
        } else {
            baseResult.success(deleteNums);
        }
        return baseResult;
    }

    /**
     * 批量新增资源位
     * @param locs
     * @return
     */
    public BaseResult addLocs(List<Loc> locs, List<Long> templates) {
        BaseResult baseResult = new BaseResult();
        int succeedCount = 0;
        for(Loc loc : locs) {
            Long insertNums = locMapper.insertLoc(loc);
            if (insertNums != null && insertNums > 0) {
                succeedCount ++;
                templateRelLocMapper.insertTemplateRelLoc(loc.getId(), templates);
            }
        }
        if (succeedCount <= 0) {
            baseResult.error("新增资源位失败！");
        } else {
            baseResult.success(succeedCount);
        }
        return baseResult;
    }

    public BaseResult editLoc(Loc loc, List<Long> templates) {
        BaseResult baseResult = new BaseResult();
        Long updateNums = locMapper.updateLoc(loc);

        // 更新资源位模板
        templateRelLocMapper.deleteTemplateRelLoc(loc.getId());
        templateRelLocMapper.insertTemplateRelLoc(loc.getId(), templates);

        if (updateNums == null) {
            baseResult.error("更新资源位失败！");
        } else {
            baseResult.success(updateNums);
        }
        return baseResult;
    }

    public BaseResult getLocBySiteId(Long siteId) {
        BaseResult baseResult = new BaseResult();
        List<Loc> locList = locMapper.selectLocBySiteId(siteId);
        System.out.println("in service ... ");
        for (Loc loc : locList) {
            System.out.println(loc.toString());
        }

        if (locList == null) {
            baseResult.error("查找该站点下的资源位失败！");
        } else {
            baseResult.success(locList);
        }
        return baseResult;
    }


    /**
     * 删除某个设备下的资源位
     * @param devId
     * @return
     */
    public BaseResult deleteLocByDev(Long devId) {
        BaseResult baseResult = new BaseResult();
        long deleteNums = locMapper.deleteLocByDev(devId);
        if (deleteNums == 0) {
            baseResult.error("删除失败！");
        } else {
            baseResult.success(deleteNums);
        }
        return baseResult;
    }


    public BaseResult getSpecificLocs(LocQuery locQuery) {
        BaseResult baseResult = new BaseResult();
        List<LocVO> locVOList = locMapper.selectSpecificLocs(locQuery);
        for (LocVO locVO : locVOList) {
            List<Long> templateIds = templateRelLocMapper.selectTemplatesByLoc(locVO.getId());
            locVO.setTemplates(templateIds);
        }
        if (locVOList == null) {
            baseResult.error("查找资源位失败！");
        } else {
            baseResult.success(locVOList);
        }
        return baseResult;
    }

    /**
     * 查找资源位所在位置
     * 0：省份 1：城市 2：区/县
     * @return
     */
    public BaseResult getLocPositions(PositionQuery positionQuery) {
        BaseResult baseResult = new BaseResult();
        List<String> positions = null;
        if (positionQuery.getType() == Global.PROVINCE) {
            positions = positionMapper.selectLocProvinces();
        }

        if (positionQuery.getType() == Global.CITY) {
            positions = positionMapper.selectLocCities(positionQuery);
        }

        if (positionQuery.getType() == Global.DISTRICT) {
            positions = positionMapper.selectLocDistricts(positionQuery);
        }

        if (positions == null) {
            baseResult.error("查找位置失败！");
        } else {
            baseResult.success(positions);
        }
        return baseResult;
    }


}
